import WPS_Enum from '../utils/defines.js'

// const WPS_Enum = new WPS_Enum()

const worksheet = {
    xl: WPS_Enum.WPS_Enum,
    //新增表格
    selectSheet(sheet_name) {
        const wb = wps.EtApplication().ActiveWorkbook
        const shts = wb.Worksheets
        for (let i = 1; i < shts.Count + 1; i++) {
            if (shts.Item(i).Name == sheet_name) {
                wb.Sheets.Item(sheet_name).Select()
                return wb.Sheets.Item(sheet_name)
            }
        }
        let new_sht = wb.Sheets.Add(undefined, wb.Sheets.Item(sheet_name), 1, -4167)
        new_sht.Name = sheet_name
        return new_sht
    },
    //新增表格并清空
    selectSheetAndClear(sheet_name) {
        const sht = this.selectSheet(sheet_name)
        sht.Rows.Item("1:65535").Delete()
        return sht
    },
    //新增获取最大行
    getRowMax(sheet_name, column_check) {
        // console.log(WPS_Enum.WPS_Enum);
        const sht = this.selectSheet(sheet_name)
        let row_max = sht.Range(column_check + "65535").End(this.xl.xlUp).Row
        if (row_max < 2) {
            row_max = 2
        }
        return row_max
    },

    //新增表格并添加标题行
    selectSheetAddTitle(sheet_name, range_address, title_arr) {
        const sht = this.selectSheetAndClear(sheet_name)
        sht.Range(range_address).Resize(1, title_arr.length).Value2 = title_arr
        // sht.Range("a:z").HorizontalAlignment = -4108
        return sht
    },

    //设置字体大小
    rangeFont(sheet_name, range_address, font_size) {
        font_size ? font_size : font_size = 12
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.Font.Size = font_size
        return rng
    },

    //设置颜色
    rangeColor(sheet_name, range_address, color) {
        color ? color : color = 65535
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.Interior.Color = color
        return rng
    },

    //设置边框
    rangeBorder(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        //内垂直线边框
        rng.Borders.Item(11).LineStyle = 1;
        rng.Borders.Item(11).TintAndShade = 1;
        rng.Borders.Item(11).ColorIndex = 0;
        // //内水平线边框
        rng.Borders.Item(12).LineStyle = 1;
        rng.Borders.Item(12).TintAndShade = 1;
        rng.Borders.Item(12).ColorIndex = 0;

        for (let i = 7; i < 11; i++) {
            //中等线宽=	-4138，细线=1 or 2，粗=4
            rng.Borders.Item(i).Weight = -4138;
            rng.Borders.Item(i).LineStyle = 1;
            rng.Borders.Item(i).TintAndShade = 1;
            rng.Borders.Item(i).ColorIndex = 0;
        }
        return rng
    },

    //设置对齐
    rangeAlign(sheet_name, range_address, align) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.VerticalAlignment = -4108
        switch (align) {
            case 'left':
                rng.HorizontalAlignment = this.xl.xlHAlignLeft
                break;
            case 'right':
                rng.HorizontalAlignment = this.xl.xlHAlignRight
                break;
            default:
                //居中
                rng.HorizontalAlignment = this.xl.xlHAlignCenter
                break;
        }
        return rng
    },

    //设置字体加粗
    rangeFontBold(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.Font.Bold = true
        return rng
    },

    //设置单元格格式
    rangeFormat(sheet_name, range_address, formatter) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        switch (formatter) {
            case 'number':
                rng.NumberFormatLocal = "_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * \"-\"??_ ;_ @_ ";
                break;
            case 'rmb':
                rng.NumberFormatLocal = "[DBNum2][$RMB]G/通用格式;[红色][DBNum2][$RMB]G/通用格式"
                break;
            case 'date':
                rng.NumberFormatLocal = 'yyyy"年"mm"月"dd"日";@'
                break;
            case 'date2':
                rng.NumberFormatLocal = 'yyyy"-"mm"-"dd;@'
                break;
            case 'week':
                rng.NumberFormatLocal = "aaaa"
                break;
            default:
                break;
        }
        return rng
    },
    rangeFit(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.ShrinkToFit = true
        return rng
    },
    rangeWrap(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.WrapText = true
        return rng
    },
    rangeComment(sheet_name, range_address, fix_method, comment_str) {
        let sht = this.selectSheet(sheet_name)
        let rng = sht.Range(range_address)
        if (fix_method == "write") {
            //单元格新增批注，原有批注覆盖
            if (!rng.Comment) {
                rng.AddComment(comment_str)
            } else {
                rng.Comment.Text(comment_str)
            }
            return rng
        }

        if (fix_method == "append") {
            //单元格追加原有批注
            if (rng.Comment) {
                let tmp = rng.Comment.Text()
                rng.Comment.Text(tmp + comment_str)
                return rng
            }
            rng.AddComment(comment_str)
            return rng
        }
    },
    colorTitle(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.Interior.Color = 49407
        rng.Interior.Pattern = this.xl.xlPatternSolid
        rng.Interior.TintAndShade = 0
    },
    colorAmount(sheet_name, range_address) {
        const sht = this.selectSheet(sheet_name)
        const rng = sht.Range(range_address)
        rng.Interior.Color = 65535
        rng.Interior.Pattern = this.xl.xlPatternSolid
        rng.Interior.TintAndShade = 0
    },
    pageSetup(sheet_name) {
        let sht = this.selectSheet(sheet_name)
        let obj = sht.PageSetup
        obj.LeftHeader = "";
        obj.CenterHeader = "";
        obj.RightHeader = "";
        obj.LeftFooter = "";
        obj.CenterFooter = "";
        obj.FirstPageNumber = 1;
        obj.RightFooter = "第 &P 页，共 &N 页";
        obj.Orientation = 1;
        obj.Zoom = 100;
        // obj.FirstPagenumber = true;
        obj.LeftMargin = 60;
        obj.RightMargin = 28;
        obj.TopMargin = 56;
        obj.BottomMargin = 28;
        obj.HeaderMargin = 14;
        obj.FooterMargin = 0;
        obj.CenterHorizontally = true;
        obj.CenterVertically = false;
        obj.PrintErrors = this.xl.xlPrintErrorsDisplayed;
        obj.Order = this.xl.xlDownThenOver;
        obj.FitToPagesWide = 1
        obj.FitToPagesTall = 0
        obj.PrintGridlines = false;
        obj.PrintHeadings = false;
        obj.BlackAndWhite = false;
        obj.PrintQuality(undefined, 600);
        obj.PaperSize = this.xl.xlPaperA4;
        obj.PrintComments = -4142;
        obj.PrintArea = "";
        obj.PrintTitleRows = "";
        obj.PrintTitleColumns = "";
    }

}


export default worksheet 